package com.xuecheng.auth.config;


import com.xuecheng.ucenter.service.impl.DaoAuthenticationProviderCustom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import org.springframework.security.crypto.password.PasswordEncoder;


/**
 * @author Mr.M
 * @version 1.0
 * @description 安全管理配置
 * @date 2022/9/26 20:53
 */
@EnableWebSecurity //开启springSecurity的自定义配置
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    DaoAuthenticationProviderCustom daoAuthenticationProviderCustom; //注入自定义的 DaoAuthenticationProvider

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(daoAuthenticationProviderCustom);//将自定义  DaoAuthenticationProvider 注入框架
    }


    /**
     * 1、创建 DBUserDetailsManager 实现 implements UserDetailsManager, UserDetailsPasswordService
     * 2、重写方法：loadUserByUsername，其中数据从数据获取
     * 3、初始化UserDetailsService ---> return new DBUserDetailsManager();
     * UserDetailsService用来管理 Spring Security 静态内部类User的用户信息
     * InMemoryUserDetailsManager是UserDetailsService的一个实现
     */
   /* @Bean
    public UserDetailsService userDetailsService() {
        //这里配置用户信息,这里暂时使用这种方式将用户存储在内存中
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        manager.createUser( //此行设置断点可以查看创建的user对象
                User.withUsername("zhangsan")
                        .password("123")
                        .authorities("p1")
                        .build());
        manager.createUser(User.withUsername("lisi").password("456").authorities("p2").build());
        return manager;
    }*/
    @Bean
    public PasswordEncoder passwordEncoder() {
//        //密码为明文方式
//        return NoOpPasswordEncoder.getInstance();
        return new BCryptPasswordEncoder(); //使用 BCrypt加密
    }

    //配置安全拦截机制
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http    //授权请求
                .authorizeRequests()
//                .antMatchers("/r/**").authenticated()//访问/r开始的请求需要认证通过
                .anyRequest().permitAll()//其它请求全部放行
                .and()
                .formLogin().successForwardUrl("/login-success");//登录成功跳转到/login-success
        http.csrf().disable(); // 如果不需要 CSRF 保护，可以禁用它
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    /*// 测试Bcryp加密使用
    public static void main(String[] args) {

        String password = "123456";
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(password);
        System.out.println(encode);

        boolean b1 = bCryptPasswordEncoder.matches("123456", encode);
        boolean b2 = bCryptPasswordEncoder.matches("123456a", encode);

        System.out.println(b1);
        System.out.println(b2);

    }*/


}
